import os

import asyncio
from autogen_agentchat.agents import AssistantAgent
from autogen_core.models import UserMessage
from autogen_ext.models.azure import AzureAIChatCompletionClient
from azure.core.credentials import AzureKeyCredential
from autogen_core import CancellationToken

from autogen_agentchat.messages import TextMessage
from autogen_agentchat.ui import Console
from IPython.display import display, HTML

client = AzureAIChatCompletionClient(
    model="Qwen/Qwen3-32B",
    endpoint="https://api.siliconflow.cn/v1",
    # To authenticate with the model you will need to generate a personal access token (PAT) in your GitHub settings.
    # Create your PAT token by following instructions here: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens
    credential=AzureKeyCredential("sk-qrcxmqxlyibljfqmkzyvdjhconjtywptojkkmujepxzniobj"),
    model_info={
        "json_output": True,
        "function_calling": True,
        "vision": False,
        "family": "unknown",
    },
)


agent = AssistantAgent(
    name="assistant",
    model_client=client,
    tools=[],
    system_message="你是一个旅行社，计划伟大的假期",
)


async def conversation():
    result = await client.create([UserMessage(content="法国的首都是哪里？/no_think", source="user")])
    if result:
        print(result)


async def assistant_run():
    # Define the query
    user_query = "给我计划一个美好的阳光假期/no_think"

    # Start building HTML output
    html_output = "<div style='margin-bottom:10px'>"
    html_output += "<div style='font-weight:bold'>User:</div>"
    html_output += f"<div style='margin-left:20px'>{user_query}</div>"
    html_output += "</div>"

    # Execute the agent response (autogen 这个方法对 上面的 qwen3 模型调用不兼容，先看看代码吧)
    response = await agent.on_messages(
        [TextMessage(content=user_query, source="user")],
        cancellation_token=CancellationToken(),
    )

    # Add agent response to HTML
    html_output += "<div style='margin-bottom:20px'>"
    html_output += "<div style='font-weight:bold'>Assistant:</div>"
    html_output += f"<div style='margin-left:20px; white-space:pre-wrap'>{response.chat_message.content}</div>"
    html_output += "</div>"

    # Display formatted HTML
    display(HTML(html_output))

# 运行异步主函数
if __name__ == "__main__":
    # asyncio.run(conversation())
    asyncio.run(assistant_run())
